<?php

class YakuMapper extends DataMapper
{
    const MODEL_CLASS = 'Yaku';

    // ------------- 更新系クエリ -----------------
    function insert(modelLoader $loader, $data)
    {
        $pdo = $this->getPDO();
        $modelClass = static::MODEL_CLASS;

        $stmt = $pdo->prepare('
            INSERT INTO yaku(slug, name, note, editor_note, sort)
            VALUES (?, ?, ?, ?, ?)
        ');
        $stmt->bindParam(1, $slug,  PDO::PARAM_STR);
        $stmt->bindParam(2, $name,  PDO::PARAM_STR);
        $stmt->bindParam(3, $note,  PDO::PARAM_STR);
        $stmt->bindParam(4, $enote, PDO::PARAM_STR);
        $stmt->bindParam(5, $sort,  PDO::PARAM_INT);

        if (! is_array($data)) $data = array($data);

        foreach ($data as $row) {
            if (! $row instanceof $modelClass || $row->isValid()) {
                throw new InvalidArgumentException;
            }
            $slug  = $row->slug;
            $name  = $row->name;
            $note  = $row->note;
            $enote = $row->editor_note;
            $sort  = $row->sort;
            $stmt->execute();
        }
    }

    function update(modelLoader $loader, $data)
    {
        $pdo = $this->getPDO();
        
        $modelClass = static::MODEL_CLASS;

        $stmt = $pdo->prepare('
            UPDATE yaku
               SET slug        = ?,
                   name        = ?,
                   note        = ?,
                   editor_note = ?,
                   sort        = ?,
                   update_date = now()
             WHERE yaku_id = ?
        ');
        $stmt->bindParam(1, $slug,  PDO::PARAM_STR);
        $stmt->bindParam(2, $name,  PDO::PARAM_STR);
        $stmt->bindParam(3, $note,  PDO::PARAM_STR);
        $stmt->bindParam(4, $enote, PDO::PARAM_STR);
        $stmt->bindParam(5, $sort,  PDO::PARAM_INT);
        $stmt->bindParam(6, $id,    PDO::PARAM_INT);
        
        if (! is_array($data)) $data = array($data);

        foreach ($data as $row) {
            if (! $row instanceof $modelClass || $row->isValid()) {
                throw new InvalidArgumentException;
            }
            $slug  = $row->slug;
            $name  = $row->name;
            $note  = $row->note;
            $enote = $row->editor_note;
            $sort  = $row->sort;
            $id    = $row->yaku_id;
            $stmt->execute();
        }
    }

    function delete(modelLoader $loader, $data)
    {
        $pdo = $this->getPDO();
        
        $modelClass = static::MODEL_CLASS;

        $stmt = $pdo->prepare('
            DELETE FROM yaku
             WHERE yaku_id = ?
        ');
        $stmt->bindParam(1, $id, PDO::PARAM_INT);

        if (! is_array($data)) $data = array($data);
        
        foreach ($data as $row) {
            if (! $row instanceof $modelClass) {
                throw new InvalidArgumentException;
            }
            $id = $row->yaku_id;
            $result = $stmt->execute();
        }
    }

    //------------- 参照系クエリ ----------------

    function findById(modelLoader $loader)
    {
        $pdo = $this->getPDO();
        
        $sql = '
            SELECT yaku_id, name, sort, slug, note, editor_note
              FROM yaku
             WHERE yaku_id = ?
        ';
        
        $stmt = $pdo->prepare($sql);
        $id = $loader->solo;
        $stmt->bindParam(1, $id, PDO::PARAM_INT);
        $stmt->execute();

        $this->_decorate($stmt, $sql);
        return $stmt->fetch();
    }


    function find(modelLoader $loader, $countMode = false)
    {
        $pdo = $this->getPDO();
        
        $order = $this->getOrderString($loader->order);
        $where = $this->getSearchString($loader->alike, 'ilike', "name||'\n'||coalesce(slug,'')||'\n'||coalesce(note,'')", $pdo);
        $limit = $this->getLimitString($loader->limit, $loader->offset);

        if (!$countMode) {
            $query = '
            SELECT
                   yaku_id, name, sort, slug, note, editor_note, update_date
              FROM yaku
                  '.$where.'
                  '.$order.'
                  '.$limit.'
            ';
            $stmt = $pdo->query($query);
            $stmt->execute();
            return $this->_decorate($stmt, $query);
            
        } else {
            $query = '
                SELECT count(yaku_id) as _count,
                       '.$loader->limit.' as _limit
              FROM yaku
                  '.$where.'
            ';
            
            $stmt = $pdo->query($query);
            $stmt->execute();
            $this->_decorate($stmt, $query);
            
            return $stmt->fetch();
        }
    }
}